package com;

import com.utils.FileUtils;

import org.zeroturnaround.zip.ZipUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileMoveRenameMain {
    private static final String ab = "/Users/apple/Desktop/androidstudio_workspace/mainTest/lib/";

    static public void main(String[] argv) {

        String ZIP_FONT = "font.zip";
        String ZIP_LOGO = "logo.zip";
        String ZIP_CONFIGURE = "configure.zip";
        String ZIP_PRINTTEXT = "printText.zip";

        String outPath = ab + "output" + File.separator;
        //压缩文件夹路径
        String zipPath = outPath + "temp.zip";
        String srcPath = ab + "ding" + File.separator + "20200927-175330.system.zip";

        String configurePath = outPath + "configure";
        String printText = outPath + "printText";
        String logo = outPath + "logo";
        String font = outPath + "font";

        //解压ZipPath压缩文件
        File desFile = new File(zipPath);
        File srcFile = new File(srcPath);
        ZipUtil.unpack(srcFile, desFile);

        boolean isCover = false;

        upZipFile(zipPath + File.separator + ZIP_CONFIGURE, configurePath, isCover);
        upZipFile(zipPath + File.separator + ZIP_PRINTTEXT, printText, isCover);
        upZipFile(zipPath + File.separator + ZIP_LOGO, logo, isCover);
        upZipFile(zipPath + File.separator + ZIP_FONT, font, isCover);

        FileUtils.deleteFile(desFile);
    }

    /**
     * 解压文件到指定目录
     *
     * @param srcPath  需解压的压缩文件
     * @param destPath 目标文件夹
     * @param isCover  是否完全覆盖
     */
    private static void upZipFile(String srcPath, String destPath, boolean isCover) {
        File srcZipFile = new File(srcPath);
        File destFile = new File(destPath);
        if (isCover) {
            if (destFile.exists()) {
                FileUtils.deleteFile(destFile);
            }
            if (srcZipFile.exists()) {
                ZipUtil.unpack(srcZipFile, destFile);
            }
        } else {
            if (!destFile.exists()) {//最终解压的目录不存在直接解压到指定文件夹ia
                if (srcZipFile.exists()) {
                    ZipUtil.unpack(srcZipFile, destFile);
                }
            } else {
                //先保存到临时文件夹
                File tempDestFile = new File(destPath + "_temp");
                if (tempDestFile.exists()) {
                    FileUtils.deleteFile(tempDestFile);
                }
                if (srcZipFile.exists()) {
                    ZipUtil.unpack(srcZipFile, tempDestFile);
                }

                if (tempDestFile.exists()) {
                    File[] fileNames = tempDestFile.listFiles();
                    for (int i = 0; i < fileNames.length; i++) {
                        String value = fileNames[i].getName();
                        if (FileUtils.isExistFile(destFile, value)) {
                            System.out.println(value + "目标目录里已存在");
                            String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
                            File renameFile = new File(destFile.getAbsolutePath() + File.separator + "副本_" + currentTime + "_" + value);
                            fileNames[i].renameTo(renameFile);
                        } else {
                            System.out.println(value + "目标目录里不存在");
                            try {
                                FileUtils.copyFile(fileNames[i].getAbsolutePath(), destFile.getAbsolutePath() + File.separator + value);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }

                if (tempDestFile.exists()) {
                    FileUtils.deleteFile(tempDestFile);
                }

            }
        }
    }


}
